#include "sdlqtrgb.h"
#include <SDL.h>
#include <QMessageBox>
#include <fstream>
#include <QDir>
#include <QSpinBox>
#include <QFileDialog>
#include "xvideo_view.h"
#include <iostream>
#include <chrono>
extern "C"
{
#include "libavutil/avutil.h"
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
}
#include <sstream>
#pragma comment(lib, "SDL2.lib")
// 存放所有的 XVideoView 对象
static std::vector<XVideoView *> views;
using namespace std::chrono_literals;
SdlQtRGB::SdlQtRGB(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    // 绑定渲染函数信号槽
    connect(this, &SdlQtRGB::ViewS, this, &SdlQtRGB::View);
    views.push_back(XVideoView::Create(XVideoView::RenderType::SDL));
    views[0]->set_win_id((void *)ui.video1->winId());
    views.push_back(XVideoView::Create(XVideoView::RenderType::SDL));
    views[1]->set_win_id((void *)ui.video2->winId());
    // startTimer(40ms);
    thread_ = std::thread(&SdlQtRGB::Main, this);
}

void SdlQtRGB::resizeEvent(QResizeEvent *ev)
{
}

SdlQtRGB::~SdlQtRGB()
{
    is_exit_ = true;
    thread_.join();
}

void SdlQtRGB::Main()
{
    while (!is_exit_)
    {
        ViewS();
        MSleep(10);
    }
}

void SdlQtRGB::View()
{
    static long long last_time[32] = {0};
    static int fps_arr[32] = {0};
    fps_arr[0] = ui.set_fps1->value();
    fps_arr[1] = ui.set_fps2->value();
    
    for (int i = 0; i < views.size(); i++)
    {
        if (fps_arr[i] <= 0)
            continue;
            
        long long current_time = NowMs();
        long long frame_duration = 1000 / fps_arr[i]; // 每帧间隔毫秒数
        
        // 检测是否到了可以渲染时间
        if (current_time - last_time[i] < frame_duration) {
            continue;
        }
        
        last_time[i] = current_time;
        auto frame = views[i]->Read();
        if (!frame)
            continue;
        views[i]->DrawFrame(frame);
        std::stringstream ss;
        ss << "FPS: " << views[i]->render_fps();
        if (i == 0)
            ui.fps1->setText(ss.str().c_str());
        else
            ui.fps2->setText(ss.str().c_str());
    }
}

void SdlQtRGB::Open1()
{
    Open(0);
}

void SdlQtRGB::Open2()
{
    Open(1);
}

void SdlQtRGB::Open(int i)
{
    QFileDialog fd(this);
    auto filename = fd.getOpenFileName(this, "选择文件");
    if (filename.isEmpty())
        return;
    std::cout << filename.toLocal8Bit().data() << std::endl;
    // 1. 打开文件
    if (!views[i]->Open(filename.toLocal8Bit().data()))
    {
        return;
    }
    int w = 0;
    int h = 0;
    QString pix;
    void *win_id = nullptr;
    if (i == 0)
    {
        w = ui.width1->value();
        h = ui.height1->value();
        win_id = (void *)ui.video1->winId();
        pix = ui.pix1->currentText();
    }
    else if (i == 1)
    {
        w = ui.width2->value();
        h = ui.height2->value();
        win_id = (void *)ui.video2->winId();
        pix = ui.pix2->currentText();
    }
    XVideoView::Format fmt = XVideoView::YUV420P;
    if (pix == "RGBA")
    {
        fmt = XVideoView::RGBA;
    }
    else if (pix == "ARGB")
    {
        fmt = XVideoView::ARGB;
    }
    else if (pix == "BGRA")
    {
        fmt = XVideoView::BGRA;
    }
    // 2. 初始化窗口和纹理
    views[i]->Init(w, h, fmt);
}
